<template>
    <Teleport to="body">
        <transition name="el-fade-in">
            <div class="leave-message-dialog" v-if="visible">
                <div class="leave-message-dialog__mask"></div>
                <div class="leave-message-dialog__content">
                    <img
                        class="icon-close"
                        :src="`${_oss_}images/icon-close-default.png`"
                        @click="onClose"
                    />

                    <div class="leave-message-title">
                        填写下表，免费获取相关信息
                    </div>

                    <div class="leave-message-decorate">
                        <img
                            :src="`${_oss_}images/dialog/leave-message/decorate.png`"
                        />
                    </div>

                    <div class="leave-mmessage-content">
                        <div class="cell">
                            <div class="cell-label">
                                姓名
                                <span class="cell-label-required">
                                    *信息保护中，请放心填写
                                </span>
                            </div>
                            <div class="cell-input">
                                <input v-model="form.name" type="text" />
                            </div>
                        </div>

                        <div class="cell">
                            <div class="cell-label">联系电话</div>
                            <div class="cell-input">
                                <input v-model="form.phone" type="text" />
                            </div>
                        </div>

                        <div class="cell">
                            <div class="cell-label">需求</div>
                            <div class="cell-input">
                                <textarea v-model="form.demand"></textarea>
                            </div>
                        </div>

                        <div class="submit-btn" @click="onSubmit">提交表单</div>
                    </div>
                </div>
            </div>
        </transition>
    </Teleport>
</template>
<script setup>
import { AddUserContact } from "@/api";
const visible = ref(false);
const form = reactive({
    name: "",
    phone: "",
    demand: "",
});
const onClose = () => {
    visible.value = false;
};

const onSubmit = async () => {
    if (isEmpty(form.name)) {
        Message().warning("请输入姓名");
    } else if (isEmpty(form.phone)) {
        Message().warning("请输入手机号");
    } else if (!/^[+-\d]{11,}$/.test(form.phone)) {
        Message().warning("请输入正确的手机号");
    } else if (isEmpty(form.demand)) {
        Message().warning("请输入需求");
    } else {
        const res = await AddUserContact({
            phone: form.phone,
            name: form.name,
            demand: form.demand,
            company: "",
        });

        if (res.code == 200) {
            form.name = "";
            form.phone = "";
            form.demand = "";
            visible.value = false;
            Message().success("留言成功，我们会有专门的人员联系您！");
        }
    }
};

defineExpose({
    show() {
        visible.value = true;
    },
});
</script>
<style lang="scss" scoped>
.leave-message-dialog {
    position: fixed;
    left: 0;
    top: 0;
    width: 100vw;
    height: 100vh;
    z-index: 999;

    &__mask {
        position: relative;
        width: 100%;
        height: 100%;
        background-color: rgba($color: #000000, $alpha: 0.5);
    }

    .icon-close {
        position: absolute;
        top: 29px;
        right: 21px;
        width: 14px;
        height: 14px;
        cursor: pointer;
    }

    &__content {
        position: absolute;
        top: 50%;
        left: 50%;

        width: 460px;
        height: 590px;
        padding: 38px 30px 33px;
        background-image: url("https://yzpt-resources.oss-cn-hangzhou.aliyuncs.com/web/images/dialog/leave-message/bg.png");
        background-size: 100% 100%;
        transform: translate(-50%, -50%);
        border-radius: 16px;
    }

    .leave-message-title {
        font-weight: 500;
        font-size: 24px;
        color: #000000;
        line-height: 30px;
    }

    .leave-message-decorate {
        margin-top: 9px;
    }

    .leave-mmessage-content {
        width: 100%;
        height: 460px;
        padding: 20px;
        background-color: #fff;
        margin-top: 13px;
        border-radius: 14px;

        .cell {
            margin-bottom: 18px;

            &-label {
                display: flex;
                align-items: center;
                justify-content: space-between;
                font-size: 18px;
                line-height: 1em;

                &-required {
                    font-size: 16px;
                    color: #ff4941;
                    line-height: 30px;
                }
            }

            &-input {
                margin-top: 12px;

                input,
                textarea {
                    font-size: 18px;
                    background: #f3f7fb;
                    border-radius: 10px;
                    border: 1px solid #e2e5f0;
                    outline: none;
                }

                input {
                    width: 100%;
                    height: 50px;
                    padding: 0 10px;
                }

                textarea {
                    width: 100%;
                    height: 108px;
                    resize: none;
                    padding: 10px;
                }
            }
        }

        .submit-btn {
            width: 100%;
            height: 56px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #5085fb;
            border-radius: 10px;
            font-weight: 500;
            font-size: 22px;
            color: #ffffff;
            line-height: 30px;
            cursor: pointer;
        }
    }
}
</style>
